package com.tuanzi.loan.schedule.receipt;

import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelRatio;
import com.tuanzi.loan.business.entity.collaborate.StatNewCollaborateRegister;
import com.tuanzi.loan.business.service.collaborate.CollaborateChannelRatioService;
import com.tuanzi.loan.business.service.collaborate.StatCollaborateCheckInLoginService;
import com.tuanzi.loan.business.service.collaborate.StatCollaborateMarketLoginService;
import com.tuanzi.loan.business.service.collaborate.StatNewCollaborateRegisterService;
import com.tuanzi.loan.business.service.statistics.CommActivityRegisterNewStatService;
import com.tuanzi.loan.core.aspect.Debug;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: liangZongXi
 * Create time: 17:53 2018/9/20
 * Description: 合作方注册表的统计
 */
@Debug
@Slf4j
@Service
@Transactional
public class StatisticsCollaborateRegisterScheduler {
    @Autowired
    private CollaborateChannelRatioService collaborateChannelRatioService;
    @Autowired
    private CommActivityRegisterNewStatService commActivityRegisterNewStatService;
    @Autowired
    private StatNewCollaborateRegisterService statNewCollaborateRegisterService;
    @Autowired
    private StatCollaborateMarketLoginService statCollaborateMarketLoginService;
    @Autowired
    private StatCollaborateCheckInLoginService statCollaborateCheckInLoginService;



    @Scheduled(cron = "${cron.statistics.collaborate.register}")
    public void statisticsCollaborateInfo(){
        List<NewCollaborateChannelRatio> allChannelRatio = collaborateChannelRatioService.findAll();
        if(allChannelRatio == null || allChannelRatio.size() <=0){
            return;
        }

        List<Integer> channelList = allChannelRatio.parallelStream().map(NewCollaborateChannelRatio::getChannelId).collect(Collectors.toList());

        LocalDate yesterday = LocalDate.now().plusDays(-1);
        Date date = new DateTime(yesterday.getYear(), yesterday.getMonthValue(), yesterday.getDayOfMonth(), 0, 0, 0).toDate();

        List<StatNewCollaborateRegister> collaborateRegisterList = commActivityRegisterNewStatService.findByChannelInAndDate(channelList, date);

        collaborateRegisterList.forEach(rest ->{
            for (NewCollaborateChannelRatio channelInfo : allChannelRatio) {
                if(rest.getChannelId().equals(channelInfo.getChannelId().toString())){
                    Integer externalNewRegisterCount = rest.getExternalNewRegisterCount();
                    Integer appActivateCount = rest.getAppActivateCount();
                    Integer appActivateCountPhone = rest.getAppActivateCountPhone();
                    Integer ratio = channelInfo.getRatio();
                    Integer externalRegisterCount = rest.getExternalRegisterCount();

                    rest.setExternalRegisterCount(Math.round((float)externalRegisterCount*ratio/100));
                    rest.setExternalNewRegisterCount(Math.round((float)externalNewRegisterCount*ratio/100));
                    rest.setAppActivateCount(Math.round((float)appActivateCount*ratio/100));
                    rest.setAppActivateCountPhone(Math.round((float)appActivateCountPhone*ratio/100));
                }
            }
        });

        if(collaborateRegisterList.size() >0){
            statNewCollaborateRegisterService.saveOrUpdate(collaborateRegisterList);
        }

        //全局渠道表-设备渠道
        try {
            statCollaborateMarketLoginService.statKlMarketLoginInfo(allChannelRatio, date);
        } catch (Exception e) {
            log.error("全局渠道表-设备渠道 扣量数据统计失败",e);
        }

        //全局渠道表-登记渠道
        try {
            statCollaborateCheckInLoginService.statKlCheckInInfo(allChannelRatio, date);
        } catch (Exception e) {
            log.error("全局渠道表-登记渠道 扣量数据统计失败",e);
        }

        collaborateChannelRatioService.updateRatioFromTomorrowRatio();
    }





}
